<html>
<head>
<title>Index.ets</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #a9b7c6;}
.s1 { color: #faa23d;}
.s2 { color: #aa7dfc;}
.s3 { color: #e2da90;}
.s4 { color: #db7e9b;}
.s5 { color: #bbb529;}
.s6 { color: #b3e54c;}
.s7 { color: #52cb54;}
</style>
</head>
<body bgcolor="#2b2b2b">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
Index.ets</font>
</center></td></tr></table>
<pre><span class="s0">import </span><span class="s1">{ </span><span class="s0">http </span><span class="s1">} </span><span class="s0">from </span><span class="s2">'@kit.NetworkKit'</span>

<span class="s0">interface IJokes </span><span class="s1">{</span>
  <span class="s0">username</span><span class="s3">: </span><span class="s0">string</span>
  <span class="s0">content</span><span class="s3">: </span><span class="s0">string</span><span class="s4">[]</span>
<span class="s1">}</span>

<span class="s5">@</span><span class="s0">Entry</span>
<span class="s5">@</span><span class="s0">Component</span>
<span class="s0">struct Index </span><span class="s1">{</span>
  <span class="s5">@</span><span class="s0">State content</span><span class="s3">: </span><span class="s0">string </span><span class="s3">= </span><span class="s2">''</span>
  <span class="s5">@</span><span class="s0">State username</span><span class="s3">: </span><span class="s0">string </span><span class="s3">= </span><span class="s2">''</span>
  <span class="s5">@</span><span class="s0">State Jokes</span><span class="s3">: </span><span class="s0">string</span><span class="s4">[] </span><span class="s3">= </span><span class="s4">[]</span>

  <span class="s0">build</span><span class="s4">() </span><span class="s1">{</span>
    <span class="s0">Row</span><span class="s4">(</span><span class="s1">{ </span><span class="s0">space</span><span class="s3">: </span><span class="s6">5 </span><span class="s1">}</span><span class="s4">) </span><span class="s1">{</span>
      <span class="s0">TextArea</span><span class="s4">(</span><span class="s1">{ </span><span class="s0">placeholder</span><span class="s3">: </span><span class="s2">'高楼平地起，评论全靠你'</span><span class="s3">, </span><span class="s0">text</span><span class="s3">: </span><span class="s0">$$this</span><span class="s3">.</span><span class="s0">content </span><span class="s1">}</span><span class="s4">)</span>
        <span class="s3">.</span><span class="s0">width</span><span class="s4">(</span><span class="s2">'80%'</span><span class="s4">)</span>
        <span class="s3">.</span><span class="s0">height</span><span class="s4">(</span><span class="s6">150</span><span class="s4">)</span>
        <span class="s3">.</span><span class="s0">onChange</span><span class="s4">((</span><span class="s0">content</span><span class="s4">) </span><span class="s3">=&gt; </span><span class="s1">{</span>
          <span class="s0">this</span><span class="s3">.</span><span class="s0">content </span><span class="s3">= </span><span class="s0">content</span>
        <span class="s1">}</span><span class="s4">)</span>
      <span class="s0">Column</span><span class="s4">(</span><span class="s1">{ </span><span class="s0">space</span><span class="s3">: </span><span class="s6">10 </span><span class="s1">}</span><span class="s4">) </span><span class="s1">{</span>
        <span class="s0">Image</span><span class="s4">(</span><span class="s0">$r</span><span class="s4">(</span><span class="s2">'app.media.ic_common_send'</span><span class="s4">))</span>
          <span class="s3">.</span><span class="s0">width</span><span class="s4">(</span><span class="s6">60</span><span class="s4">)</span>
        <span class="s0">Text</span><span class="s4">(</span><span class="s2">'发布'</span><span class="s4">)</span>
          <span class="s3">.</span><span class="s0">fontSize</span><span class="s4">(</span><span class="s6">28</span><span class="s4">)</span>
          <span class="s3">.</span><span class="s0">fontColor</span><span class="s4">(</span><span class="s2">'#FF411B'</span><span class="s4">)</span>
          <span class="s3">.</span><span class="s0">onClick</span><span class="s4">(() </span><span class="s3">=&gt; </span><span class="s1">{</span>
            <span class="s0">const req </span><span class="s3">= </span><span class="s0">http</span><span class="s3">.</span><span class="s0">createHttp</span><span class="s4">()</span>
            <span class="s0">req</span><span class="s3">.</span><span class="s0">request</span><span class="s4">(</span><span class="s2">'https://hmajax.itheima.net/api/addcmt'</span><span class="s3">, </span><span class="s1">{</span>
              <span class="s0">method</span><span class="s3">: </span><span class="s0">http</span><span class="s3">.</span><span class="s0">RequestMethod</span><span class="s3">.</span><span class="s0">POST</span><span class="s3">,</span>
              <span class="s0">extraData</span><span class="s3">: </span><span class="s2">`username=张三&amp;content=</span><span class="s1">${</span><span class="s0">this</span><span class="s3">.</span><span class="s0">content</span><span class="s1">}</span><span class="s2">&amp;time=2021-09-24 15:07:16`</span>
            <span class="s1">}</span><span class="s4">)</span>
              <span class="s3">.</span><span class="s0">then</span><span class="s4">((</span><span class="s0">res</span><span class="s4">) </span><span class="s3">=&gt; </span><span class="s1">{</span>
                <span class="s7">// const result = JSON.parse(res.result as string) as IJokes</span>
                <span class="s7">// this.Jokes = result.content</span>
                <span class="s0">AlertDialog</span><span class="s3">.</span><span class="s0">show</span><span class="s4">(</span><span class="s1">{ </span><span class="s0">message</span><span class="s3">: </span><span class="s0">JSON</span><span class="s3">.</span><span class="s0">stringify</span><span class="s4">(</span><span class="s0">res</span><span class="s3">.</span><span class="s0">result</span><span class="s3">, </span><span class="s0">null</span><span class="s3">, </span><span class="s6">2</span><span class="s4">) </span><span class="s1">}</span><span class="s4">)</span>
                <span class="s7">// AlertDialog.show({ message: JSON.stringify(this.Jokes, null, 2) })</span>
              <span class="s1">}</span><span class="s4">)</span>
          <span class="s1">}</span><span class="s4">)</span>
      <span class="s1">}</span>
    <span class="s1">}</span>
    <span class="s3">.</span><span class="s0">padding</span><span class="s4">(</span><span class="s1">{ </span><span class="s0">left</span><span class="s3">: </span><span class="s6">10</span><span class="s3">, </span><span class="s0">right</span><span class="s3">: </span><span class="s6">10 </span><span class="s1">}</span><span class="s4">)</span>
  <span class="s1">}</span>
<span class="s1">}</span></pre>
</body>
</html>